package com.lykj.llw.box.utils

import android.R
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi


object NotificationUtil {
    private const val CHANNEL_ONE_ID = "攒趣" //唯一性
    private const val NOTIFY_ID = 0x1111
    private const val FOREGROUND_ID = 0x1112

    /**
     * 公开使用
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    fun sendNotification(
        context: Context,
        channelName: String,
        channelDesc: String,
        contentTitle: String,
        contentText: String
    ) {
        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.notify(
                NOTIFY_ID,
                getNotificationO(
                    context,
                    manager,
                    channelName,
                    channelDesc,
                    contentTitle,
                    contentText
                )
            )
        } else {
            manager.notify(NOTIFY_ID, getNotification(context, contentTitle, contentText))
        }
    }

    /**
     * onStartCommand调用
     */
    fun startForeground(
        service: Service,
        channelName: String,
        channelDesc: String,
        contentTitle: String,
        contentText: String
    ) {
        val manager = service.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notification: Notification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getNotificationO(service, manager, channelName, channelDesc, contentTitle, contentText)
        } else {
            getNotification(service, contentTitle, contentText)
        }
        notification.flags = Notification.FLAG_ONGOING_EVENT
        notification.flags = notification.flags or Notification.FLAG_NO_CLEAR
        notification.flags = notification.flags or Notification.FLAG_FOREGROUND_SERVICE
        service.startForeground(FOREGROUND_ID, notification)
    }

    /**
     * onDestory之前调用
     */
    fun stopForegound(service: Service) {
        service.stopForeground(true)
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private fun getNotificationO(
        context: Context,
        manager: NotificationManager,
        name: String,
        desc: String,
        contentTitle: String,
        contentText: String
    ): Notification {
        val channel = NotificationChannel(
            CHANNEL_ONE_ID, name,
            NotificationManager.IMPORTANCE_DEFAULT
        )
        channel.description = desc

        //channel.enableLights(true);
        //channel.setLightColor(color);

        //Uri mUri = Settings.System.DEFAULT_NOTIFICATION_URI;
        //channel.setSound(mUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);

        // Register the channel with system; you can't change the importance
        // or other notification behaviors after this
        manager.createNotificationChannel(channel)
        val builder: Notification.Builder = Notification.Builder(context, CHANNEL_ONE_ID)
        builder.setCategory(Notification.CATEGORY_RECOMMENDATION)
            .setContentTitle(contentTitle)
            .setContentText(contentText) //.setContentIntent(getPendingIntent(context))
            .setSmallIcon(R.drawable.ic_notification_overlay)
        return builder.build()
    }

    private fun getNotification(
        context: Context,
        contentTitle: String,
        contentText: String
    ): Notification {
        val builder = Notification.Builder(context)
            .setPriority(Notification.PRIORITY_DEFAULT) //.setLights(color, 1000, 0)
        //.setSound(null, null);
        builder.setCategory(Notification.CATEGORY_RECOMMENDATION)
            .setContentTitle(contentTitle)
            .setContentText(contentText) //.setContentIntent(getPendingIntent(context))
            .setSmallIcon(R.drawable.ic_notification_overlay)
        return builder.build()
    } //    private static PendingIntent getPendingIntent(Context context) {
    //        Intent intent = new Intent(context, MainActivity.class);
    //        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    //        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //        return pi;
    //    }
}